/**
 * Copyright 2021 xpstem.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.xpstem.lelink.bluetooth;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

/**
 * @author billy zhang
 */
public class BtDeviceInfo implements Parcelable {

    public android.bluetooth.BluetoothDevice device;
    public int bonded;
    public int rssi;
    public byte[] scanRecord;

    public BtDeviceInfo(android.bluetooth.BluetoothDevice device) {
        this(device, 0, 0, null);
    }

    public BtDeviceInfo(android.bluetooth.BluetoothDevice device, int bonded, int rssi, byte[] scanRecord) {
        this.device = device;
        this.bonded = bonded;
        this.rssi = rssi;
        this.scanRecord = scanRecord;
    }

    public int getBonded() { return bonded; }

    public String getName() {
        String name = device.getName();
        name = TextUtils.isEmpty(name) ? getAddress() : name;
        if (bonded == 0) {
            return name;
        }
        return name + "（已配对）";
    }

    public String getAddress() {
        return device != null ? device.getAddress() : "";
    }

    public int getRssi() {
        return rssi;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        StringBuilder sb = new StringBuilder();
        sb.append(", mac = " + device.getAddress());
        return sb.toString();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeParcelable(device, 0);
        dest.writeInt(bonded);
        dest.writeInt(rssi);
        dest.writeByteArray(scanRecord);
    }

    public BtDeviceInfo(Parcel in) {
        device = in.readParcelable(android.bluetooth.BluetoothDevice.class.getClassLoader());
        bonded = in.readInt();
        rssi = in.readInt();
        scanRecord = in.createByteArray();
    }

    public static final Creator<BtDeviceInfo> CREATOR = new Creator<BtDeviceInfo>() {
        public BtDeviceInfo createFromParcel(Parcel source) {
            return new BtDeviceInfo(source);
        }

        public BtDeviceInfo[] newArray(int size) {
            return new BtDeviceInfo[size];
        }
    };

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        BtDeviceInfo that = (BtDeviceInfo) o;

        return device.equals(that.device);
    }

    @Override
    public int hashCode() {
        return device.hashCode();
    }
}
